//相当于修饰Java的静态方法
@Synchronized
fun testA() {
    Thread.sleep(1000L)
    println("${Thread.currentThread().name}-testA()")
}

//相当于修饰Java的静态方法
@Synchronized
fun testB() {
    Thread.sleep(1000L)
    println("${Thread.currentThread().name}-testB()")
}

class SynchronizedTest {

    //相当于修饰Java的普通方法
    @Synchronized
    fun testC(time: Long) {
        Thread.sleep(time)
        println("${Thread.currentThread().name}-testC()")
    }

    //相当于修饰Java的普通方法
    @Synchronized
    fun testD() {
        Thread.sleep(1000L)
        println("${Thread.currentThread().name}-testD()")
    }

    //testD()与testE()的写法是等价的
    fun testE() {
        //修饰代码块
        synchronized(this) {
            Thread.sleep(1000L)
            println("${Thread.currentThread().name}-testE()")
        }
    }

    fun testF() {
        //修饰代码块
        synchronized(this) {
            Thread.sleep(1000L)
            println("${Thread.currentThread().name}-testF()")
        }
    }
}

fun main() {
    Thread {
        testA()
    }.start()

    Thread {
        testB()
    }.start()

    Thread.sleep(3000L)

    val a = SynchronizedTest()

    val b = SynchronizedTest()

    Thread {
        println("a-${Thread.currentThread().name}-start")
        a.testC(3000)
        println("a-${Thread.currentThread().name}-end")
    }.start()

    Thread {
        println("b-${Thread.currentThread().name}-start")
        b.testC(1000)
        println("b-${Thread.currentThread().name}-end")
    }.start()

    Thread {
        println("a-${Thread.currentThread().name}-start")
        a.testD()
        println("a-${Thread.currentThread().name}-end")
    }.start()

    Thread.sleep(8000L)

    Thread {
        a.testE()
    }.start()

    Thread {
        a.testF()
    }.start()
}